<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/3/14
 * Time: 20:58
 */

namespace app\Module\Index\Controller;


use app\Module\Index\Logic\MessageLogic;
use app\Module\Index\Logic\MessageType;
use app\Module\Index\Model\CirclePhotoCommentModel;
use app\Module\Index\Model\CirclePhotoModel;
use app\Module\Index\Model\CirclePhotoReplyModel;
use app\Module\Index\Model\UnreadCirclePhotoCommentModel;
use app\Module\Index\Model\UserModel;

class CirclePhotoCommentController extends CheckAuth
{
    /**
     * 添加评论
     * @verify int      $circle_photo_id
     * @verify string   $content
     */
    public function addAction($circle_photo_id, $content)
    {
        // 新增评论
        $circlePhotoCommentModel = new CirclePhotoCommentModel();
        $id                      = $circlePhotoCommentModel->insert([
            'circle_photo_id' => $circle_photo_id,
            'content'         => $content,
            'user_id'         => $this->userId,
            'created'         => now(),
        ]);


        $circlePhotoModel = new CirclePhotoModel();
        $circlePhotoModel->update('comment_num=comment_num+1', ['id' => $circle_photo_id]);

        // 新增未读
        $data      = $circlePhotoModel->row(['id' => $circle_photo_id]);
        $userModel = new UserModel();
        $userData  = $userModel->row([
            'circle_id' => $data['circle_id'],
        ]);

        if ($this->userId != $userData['id']) {
            $unreadCirclePhotoCommentModel = new UnreadCirclePhotoCommentModel();
            $unreadCirclePhotoCommentModel->addSet([
                'updated'                      => now(),
                'last_circle_photo_comment_id' => $id,
            ]);
            $result = $unreadCirclePhotoCommentModel->update('num=num+1', [
                'user_id'         => $this->userId,
                'target_user_id'  => $userData['id'],
                'circle_photo_id' => $circle_photo_id,
            ]);
            if (empty($result)) {
                $unreadCirclePhotoCommentModel->insert([
                    'target_user_id'               => $userData['id'],
                    'last_circle_photo_comment_id' => $id,
                    'user_id'                      => $this->userId,
                    'created'                      => now(),
                    'updated'                      => now(),
                    'num'                          => 1,
                    'circle_photo_id'              => $circle_photo_id,
                ]);
            }
        }


        if ($id) {
            $messageLogic = new MessageLogic();
            $messageLogic->add($this->userId, $userData['id'], $circle_photo_id, MessageType::CIRCLE_PHOTO_COMMENT, $data['title']);

            return success();
        } else {
            return fail('评论失败');
        }
    }

    /**
     * 获取评论列表
     *
     * @verify int $circle_photo_id
     * @verify int $page
     * @verify int $limit
     */
    public function getListAction($circle_photo_id, $page = 1, $limit = 20)
    {
        $circlePhotoCommentModel = new CirclePhotoCommentModel();
        $circlePhotoCommentModel->addWhere([
            'circle_photo_id' => $circle_photo_id,
        ]);
        $count = $circlePhotoCommentModel->count();


        $circlePhotoCommentModel->setFields([
            'circle_photo_comment.id,user_id,user.username,user.avatar_url,circle_photo_comment.created,circle_photo_comment.content',
        ]);
        $circlePhotoCommentModel->setJoin('user', 'user.id = circle_photo_comment.user_id', 'LEFT JOIN');
        $circlePhotoCommentModel->addWhere([
            'circle_photo_id' => $circle_photo_id,
        ]);
        $circlePhotoCommentModel->setPage($page, $limit);
        $circlePhotoCommentModel->addOrder('circle_photo_comment.created', false);
        $data = $circlePhotoCommentModel->select();

        $circlePhotoReplyModel = new CirclePhotoReplyModel();
        foreach ($data as $key => $datum) {
            $circlePhotoReplyModel->addWhere([
                'circle_photo_comment_id' => $datum['id'],
            ]);
            $replyCount = $circlePhotoReplyModel->count();

            $circlePhotoReplyModel->setFields([
                'user_id,target_user_id,target_user.username as target_user_username,user.username,circle_photo_reply.content,circle_photo_reply.created',
            ]);
            $circlePhotoReplyModel->addWhere([
                'circle_photo_comment_id' => $datum['id'],
            ]);
            $circlePhotoReplyModel->setJoin('user', 'user.id = circle_photo_reply.user_id', 'LEFT JOIN');
            $circlePhotoReplyModel->setJoin('user as target_user', 'target_user.id = circle_photo_reply.target_user_id', 'LEFT JOIN');
            $circlePhotoReplyModel->addOrder('circle_photo_reply.created', true);
            $list                     = $circlePhotoReplyModel->select();
            $data[$key]['reply']      = $list;
            $data[$key]['replyCount'] = $replyCount;
        }

        return success(['count' => $count, 'data' => $data]);
    }

    /**
     * 更新未读信息
     *
     * @verify int $user_id
     * @verify int $circle_photo_id
     * @return string
     */
    public function updateUnreadAction($user_id, $circle_photo_id)
    {
        // 更新消息为已读
        $unreadCirclePhotoCommentModel = new UnreadCirclePhotoCommentModel();
        $unreadCirclePhotoCommentModel->addWhere('num', '0', '>');
        $unreadCirclePhotoCommentModel->update(
            [
                'num'     => 0,
                'updated' => now(),
            ],
            [
                'user_id'         => $user_id,
                'circle_photo_id' => $circle_photo_id,
                'target_user_id'  => $this->userId,
            ]);

        return success();
    }

    /**
     * 获取未读评论
     *
     * @verify int|null $page
     * @verify int|null $limit
     * @return string
     */
    public function getUnreadAction($page = 1, $limit = 20)
    {
        $unreadCirclePhotoCommentModel = new UnreadCirclePhotoCommentModel();
        $unreadCirclePhotoCommentModel->setFields('user.username,user.avatar_url,unread_circle_photo_comment.num,unread_circle_photo_comment.id,circle_photo_comment.content,circle_photo_comment.created,circle_photo_comment.id as circle_photo_comment_id,unread_circle_photo_comment.circle_photo_id');
        $unreadCirclePhotoCommentModel->addWhere([
            'target_user_id' => $this->userId,
        ]);

        $unreadCirclePhotoCommentModel->setJoin('circle_photo_comment', 'circle_photo_comment.id=unread_circle_photo_comment.last_circle_photo_comment_id');
        $unreadCirclePhotoCommentModel->setJoin('user', 'user.id=circle_photo_comment.user_id');
        $unreadCirclePhotoCommentModel->addOrder('circle_photo_comment.created', false);
        $unreadCirclePhotoCommentModel->setPage($page, $limit);
        $data = $unreadCirclePhotoCommentModel->select();

        return success($data);
    }
}